﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using B10_Ex02_Elad_034695114_Meyron_026607788;
using Microsoft.Xna.Framework.Graphics;

namespace B10_Ex02_EladLebovitch_034695114.ObjectModel
{
    public class HumanPlayer
    {
        // Events
        public event EventHandler PlayerIsDeadEvent;
        public event EventHandler PlayerScoreChangedEvent;

        #region Consts
        private const int k_SoulLossPenalty = 2200;
        private const string k_ShipAssetStartName = @"Sprites\Ship0";
        private const string k_ShipAssetSuffix = @"_32x32";
        #endregion

        #region Data Members
        public Keys LeftMoveKey { get; set; }
        public Keys RightMoveKey { get; set; }
        public Keys FireKey { get; set; }
        public int PlayerNumber { get; set; }
        public SpaceShip PlayerShip { get; set; }
        private int m_GameScore;

        /// <summary>
        /// Current game score
        /// </summary>
        public int GameScore
        {
            get
            {
                return m_GameScore;
            }

            set
            {
                m_GameScore = (int)MathHelper.Clamp((float)value, 0f, float.MaxValue);
            }
        }

        /// <summary>
        /// The number of souls
        /// </summary>
        public int Souls
        {
            get;
            set;
        }

        #endregion

        /// <summary>
        /// Inits a human player class
        /// </summary>
        /// <param name="i_Game"></param>
        /// <param name="i_LeftMove"></param>
        /// <param name="i_RightMove"></param>
        /// <param name="i_FireKey"></param>
        /// <param name="i_PlayerNumber"></param>
        /// <param name="i_StartLocationOffset"></param>
        public HumanPlayer(Game i_Game, Keys i_LeftMove, Keys i_RightMove, 
                           Keys i_FireKey, int i_PlayerNumber, int i_StartLocationOffset, bool i_MouseControl)
        {
            // Set the keys
            LeftMoveKey = i_LeftMove;
            RightMoveKey = i_RightMove;
            FireKey = i_FireKey;
            
            // Set player detais
            PlayerNumber = i_PlayerNumber;

            // Init and set the ship
            PlayerShip = new SpaceShip(i_Game, k_ShipAssetStartName + i_PlayerNumber.ToString() + k_ShipAssetSuffix, i_MouseControl);
            PlayerShip.PlayerShipDestroyedEvent += new PlayerShipDestroyed(PlayerShip_PlayerShipDestroyedEvent);
            PlayerShip.PlayerShipScoredEvent += new PlayerShipScored(PlayerShip_PlayerShipScoredEvent); 
            PlayerShip.LeftMoveKey = LeftMoveKey;
            PlayerShip.RightMoveKey = RightMoveKey;
            PlayerShip.FireKey = FireKey;
            PlayerShip.StartLocationOffset = i_StartLocationOffset;
        }

        private void PlayerShip_PlayerShipScoredEvent(object sender, SpaceShipScoredEventArgs e)
        {
            // Update the points for this player
            this.GameScore += e.Points;

            // Check if someone wants to know
            if (PlayerScoreChangedEvent != null)
            {
                PlayerScoreChangedEvent(this, new EventArgs());
            }
        }

        /// <summary>
        /// What happens when the ship is destroyed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlayerShip_PlayerShipDestroyedEvent(object sender, EventArgs e)
        {
            // Set the ship destroyed details:
            this.Souls--;
            this.GameScore -= k_SoulLossPenalty;
            
            // Check if we are still alive or is it game over for this player
            if (Souls > 0)
            {
                // Reinit the ship and start a new life
                this.PlayerShip.PositionOfOrigin = new Vector2(0, this.PlayerShip.PositionOfOrigin.Y);
            }
            // if no more souls
            else
            {
                // The ship is gone
                this.PlayerShip.Distroy();

                // Check if someone wants to know
                if (PlayerIsDeadEvent != null)
                {
                    PlayerIsDeadEvent(this, new EventArgs());
                }
            }
        }
    }
}
